﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Launch : MonoBehaviour
{
    public Sprite showSprite;
    public Sprite buildSprite;
    public GameObject foodCupboardPrefab;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(LoadEntityResource());
    }

    public IEnumerator LoadEntityResource()
    {
        var uwr = UnityWebRequestAssetBundle.GetAssetBundle($"{Application.persistentDataPath}/Mod/{FoodCupboardMod.modName}/Res/foodcupboardimage.ab");
        yield return uwr.SendWebRequest();

        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        var loadAsset = bundle.LoadAllAssets();
        foreach (var item in loadAsset)
        {
            if (item is Sprite sprite)
            {
                if (sprite.name == "FoodCupboard_0")
                {
                    showSprite = sprite;
                }
                else if (sprite.name == "FoodCupboard_1")
                {
                    buildSprite = sprite;
                }
            }
        }

        uwr = UnityWebRequestAssetBundle.GetAssetBundle($"{Application.persistentDataPath}/Mod/{FoodCupboardMod.modName}/Res/foodcupboard.ab");
        yield return uwr.SendWebRequest();

        bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        loadAsset = bundle.LoadAllAssets();
        foreach (var item in loadAsset)
        {
            if (item is GameObject obj)
            {
                foodCupboardPrefab = obj;
            }
        }
        var foodCupboard = foodCupboardPrefab.GetComponent<FoodCupboard>();
        foodCupboard.id = ConfigurationManager.Instance.ConstructionConfigs.Count;

        var demand = new DemandResource
        {
            resourceId = 4,
            count = 20
        };
        var sheet = new BuildSheet
        {
            buildDemandResources = new List<DemandResource>() { demand }
        };
        var conConfig = new ConstructionConfig
        {
            fullName = "食物自助机",
            id = foodCupboard.id,
            construction = foodCupboard,
            showSprite = showSprite,
            buildSprite = buildSprite,
            collisions = new List<Vector2Int>() { Vector2Int.zero, Vector2Int.up },
            buildSheets = new List<BuildSheet>() { sheet }
        };
        ConfigurationManager.Instance.ConstructionConfigs.Add(conConfig.id, conConfig);

        var techConfig = new TechTreeNodeConfig
        {
            id = ConfigurationManager.Instance.TechTreeConfigs.Count,
            title = "Mod建筑",
            explain = "",
            kind = TechTreeNodeKind.Survival,
            constructionIds = new List<int>() { conConfig.id },
            beforeNodeIds = new List<int>(),
            afterNodeIds = new List<int>()
        };
        ConfigurationManager.Instance.TechTreeConfigs.Add(techConfig.id, techConfig);
        Destroy(gameObject);
    }
}
